package com.jilefojing.dao.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.jilefojing.beans.enums.ItemTypeEnumForSource;
import com.jilefojing.beans.pojos.RHotTag;
import com.jilefojing.beans.query.RHotTagQuery;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.dao.IRHotTagDao;
import com.jilefojing.dao.base.impl.BaseDao;
import com.jilefojing.dao.mapper.IRHotTagMapper;
import com.jilefojing.util.Detect;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年07月29日 13时54分55秒
 */
@Repository
public class RHotTagDao extends BaseDao<RHotTag> implements IRHotTagDao {
	@Autowired
	private IRHotTagMapper rHotTagMapper;

	@Override
	public Pagination<RHotTag> getPagination(RHotTagQuery rHotTagQuery, Pagination<RHotTag> pagination) {

		Example example = new Example(RHotTag.class);
		Criteria criteria = example.createCriteria();
		if (Detect.notEmpty(rHotTagQuery.getIds())) {
			criteria.andIn(RHotTag.FIELD_ID, rHotTagQuery.getIds());
		}
		if (Detect.notEmpty(rHotTagQuery.getItemTypes())) {
			List<Short> itemTypeCodes = new ArrayList<Short>();
			for (ItemTypeEnumForSource _ItemType : rHotTagQuery.getItemTypes()) {
				itemTypeCodes.add(_ItemType.getCode());
			}
			criteria.andIn(RHotTag.FIELD_ITEMTYPE, itemTypeCodes);
		}
		if (Detect.notEmpty(rHotTagQuery.getTagIds())) {
			criteria.andIn(RHotTag.FIELD_TAGID, rHotTagQuery.getTagIds());
		}
		if (Detect.notEmpty(rHotTagQuery.getCreateUserIds())) {
			criteria.andIn(RHotTag.FIELD_CREATEUSER, rHotTagQuery.getCreateUserIds());
		}

		if (Detect.notEmpty(rHotTagQuery.getCreateUserIds())) {
			criteria.andIn(RHotTag.FIELD_CREATEUSER, rHotTagQuery.getCreateUserIds());
		}

		if (rHotTagQuery.getEqualCreateTime() != null) {
			criteria.andEqualTo(RHotTag.FIELD_CREATETIME, rHotTagQuery.getEqualCreateTime());
		}

		if (rHotTagQuery.getEqualAndBeforCreateTime() != null) {
			criteria.andGreaterThanOrEqualTo(RHotTag.FIELD_CREATETIME, rHotTagQuery.getEqualAndBeforCreateTime());
		}

		if (rHotTagQuery.getEqualAndAfterCreateTime() != null) {
			criteria.andLessThanOrEqualTo(RHotTag.FIELD_CREATETIME, rHotTagQuery.getEqualAndAfterCreateTime());
		}

		if (rHotTagQuery.getBeforCreateTime() != null) {
			criteria.andGreaterThan(RHotTag.FIELD_CREATETIME, rHotTagQuery.getBeforCreateTime());
		}

		if (rHotTagQuery.getAfterCreateTime() != null) {
			criteria.andLessThan(RHotTag.FIELD_CREATETIME, rHotTagQuery.getAfterCreateTime());
		}

		if (Detect.notEmpty(pagination.getOrders())) {
			example.setOrderByClause(pagination.getOrdersToStr());
		}

		return super.getPagination(pagination, example);
	}

	@Override
	public void deleteByTagId(long tagId) {
		this.deleteByTagIds(Arrays.asList(new Long[] { tagId }));
	}

	@Override
	public void deleteByTagIds(List<Long> tagIds) {
		Example example = new Example(RHotTag.class);
		example.createCriteria().andIn(RHotTag.FIELD_TAGID, tagIds);
		super.deleteByExample(example);
	}

	@Override
	public long countByTagId(long tagId) {
		return this.countByTagIds(Arrays.asList(new Long[] { tagId }));
	}

	@Override
	public long countByTagIds(List<Long> tagIds) {
		Example example = new Example(RHotTag.class);
		example.createCriteria().andIn(RHotTag.FIELD_TAGID, tagIds);
		return super.countByExample(example);
	}
}
